/*
 * @Description: 
 * @Author: 葛玉根
 * @Date: 2021-03-06 10:43:56
 * @LastEditTime: 2021-03-08 01:00:46
 * @LastEditors: 葛玉根
 */
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { classDataApi, allRoomDataApi, allCurriculumDataApi, editClassApi,addClassApi, deleClassApi} from '@/api/classAdministrationApi';
import { classDataType, curriculumDataType, roomDataType} from '@/interface/classAdministration';

export interface ClassModelState {
    data: classDataType[];
    roomData: roomDataType[];
    curriculumData: curriculumDataType[];
}

export interface ClassModelType {
    namespace: 'classAdministration';
    state: ClassModelState;
    effects: {
        getClassData: Effect;
        getRoomData: Effect;
        getCurriculumData: Effect;
        addClass: Effect;
        deleClass: Effect;
        editClass:Effect
    };
    reducers: {
        save: Reducer<ClassModelState>;
        // 启用 immer 之后
        // save: ImmerReducer<ClassModelState>;
    };
    subscriptions: { setup: Subscription };
}

const IndexModel: ClassModelType = {
    namespace: 'classAdministration',

    state: {
        data: [],
        roomData: [],
        curriculumData:[]
    },

    effects: {
        *getClassData({ payload }, { call, put }) {
            let res = yield call(classDataApi)
            if (res.code === 1) {
                yield put({
                    type: 'save',
                    payload: { data:res.data }
                })
            }
        },
        *getRoomData({ payload }, { call, put }) {
            let res = yield call(allRoomDataApi)
            if (res.code === 1) {
                yield put({
                    type: 'save',
                    payload: { roomData: res.data }
                })
            }
        },
        *getCurriculumData({ payload }, { call, put }) {
            let res = yield call(allCurriculumDataApi)
            if (res.code === 1) {
                yield put({
                    type: 'save',
                    payload: { curriculumData: res.data }
                })
            }
        },
        *addClass({ payload }, { call, put }) {
            let res = yield call(addClassApi, payload)
            if (res.code === 1) {
                yield put({
                    type: 'getClassData',
                })
            }
        },
        *deleClass({ payload }, { call, put }) {
            let res = yield call(deleClassApi, payload)
            if (res.code === 1) {
                yield put({
                    type: 'getClassData',
                })
            }
        },
        *editClass({ payload }, { call, put }) {
            let res = yield call(editClassApi, payload)
            console.log(res)
            if (res.code === 1) {
                yield put({
                    type: 'getClassData',
                })
            }
        },
    },
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
        // 启用 immer 之后
        // save(state, action) {
        //   state.name = action.payload;
        // },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                // if (pathname === '/') {
                //     dispatch({
                //         type: 'query',
                //     })
                // }
            });
        }
    }
};

export default IndexModel;
